<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <el-tree
          :data="list"
          :props="{
            children: 'children',
            label: 'name'
          }"
          default-expand-all
        >
          <template #default="{data}">
            <!-- 用一个行列布局 -->
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
              <el-col :span="20">
                <svg-icon icon-class="good" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length===0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit ? '编辑':'新增'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <AddorEdit
          v-if="showDialog"
          :get-id="curId"
          :is-edit="isEdit"
          @success="hSuccess"
          @close="showDialog=false"
        />
      </el-dialog>

    </div>
  </div>
</template>
<script>
// 依赖一份树形数据
import { getDepartment, delDepartment } from '@/api/departments'
import { transDataToTreeData } from '@/utils/index'
import AddorEdit from './Deptdialog.vue'
export default {
  components: { AddorEdit },
  data() {
    return {
      list: [],
      showDialog: false, // 默认弹层是关闭的
      curId: '', // 存放新增部门的id
      isEdit: false // isEdit来控制编辑还是新增状态,false是新增状态,true是编辑状态
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    // 重置表单,清空校验信息
    hClose() {
      this.$refs.deptForm.resetForm()
    },

    // 删除部门
    hDel(id) {
      // console.log(1)

      this.$confirm('你确定要删除吗?').then(async() => {
        const res = await delDepartment(id)
        console.log(res)
        console.log('删除的', res)
        this.loadDepartment()
      })
        .catch(err => err)
    },
    // 编辑部门
    hEdit(id) {
      this.curId = id
      this.isEdit = true // 编辑状态
      this.showDialog = true // 打开弹层
    },
    // 子传父事件
    hSuccess() {
      // 关闭弹层 更新数据
      this.showDialog = false
      this.loadDepartment()
    },
    // 新增部门
    hAdd(id) {
      this.curId = id
      this.isEdit = false // 新增状态
      this.showDialog = true
    },
    // 查询部门
    async loadDepartment() {
      try {
        const res = await getDepartment()
        // console.log(11222)
        console.log('查询部门', res)
        // 删除第一条数据
        res.data.depts.shift()
        this.list = res.data.depts
        this.list = transDataToTreeData(res.data.depts)
        console.log('树装', this.list)
      } catch (error) {
        console.log(error)
      }
    }
  }

}

</script>

